#include "udpServer.hpp"
#include <memory>
#include <unordered_map>
#include <fstream>
#include <signal.h>

using namespace Server;
using namespace std;

static void Usage(string proc)
{
    cout << "Usage:\n\t" << proc << " local_port\n\n";
}

void routeMessage(const int &sockfd, string clientip, uint16_t clientport, string message)
{
    struct sockaddr_in client;
    bzero(&client,sizeof(client));

    client.sin_addr.s_addr = inet_addr(clientip.c_str());
    client.sin_family = AF_INET;
    client.sin_port = htons(clientport);

    string response = message;
    cout << message << endl;
    response += "[server return]";
    sendto(sockfd,response.c_str(),response.size(),0,(struct sockaddr*)&client,sizeof(client));
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }
    uint16_t port = atoi(argv[1]); // 用于将字符串转换为整数。

    unique_ptr<udpServer> usvr(new udpServer(routeMessage, port));

    usvr->initServer();
    usvr->start();

    return 0;
}